/*
* @Author: Adolphus.Yao
* @Date:   2019-08-27 09:52:12
* @Last Modified by:   Adolphus.Yao
* @Last Modified time: 2019-08-27 09:57:56
*/
package main

import (
    "fmt"
    "net"
    "strings"
)

func connHandler(c net.Conn) {
    if c == nil {
        return
    }
    buf := make([]byte, 4096)
    for {
        cnt, err := c.Read(buf)
        if err != nil || cnt == 0 {
            c.Close()
            break
        }
        inStr := strings.TrimSpace(string(buf[0:cnt]))
        inputs := strings.Split(inStr, " ")
        switch inputs[0] {
        case "ping":
            c.Write([]byte("pong\n"))
        case "echo":
            echoStr := strings.Join(inputs[1:], " ") + "\n"
            c.Write([]byte(echoStr))
        case "quit":
            c.Close()
            break
        default:
            fmt.Printf("Unsupported command: %s\n", inputs[0])
        }
    }
    fmt.Printf("Connection from %v closed. \n", c.RemoteAddr())
}

func main() {
    server, err := net.Listen("tcp", ":8208")
    if err != nil {
        fmt.Printf("Fail to start server, %s\n", err)
    }
    fmt.Println("Server Started ...")
    for {
        conn, err := server.Accept()
        if err != nil {
            fmt.Printf("Fail to connect, %s\n", err)
            break
        }
        go connHandler(conn)
    }
}